package com.safe.ele.util;

import com.alibaba.druid.sql.visitor.functions.Concat;
import com.safe.ele.config.SystemConfig;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

/**
 * @Author wjw
 * @create 2021/1/13 14:56
 */
public class RedisUtil {
    public static RedissonClient client;
    static {
        Config config=new Config();
        config.useSingleServer().setAddress("redis://"+ SystemConfig.REDIS_HOST +":"+SystemConfig.REDIS_PORT).setPassword(SystemConfig.REDIS_PASS);
        client = Redisson.create(config);
    }
    //新增
    public static void setStr(String key,Object val,int seconds){
        client.getBucket(key).set(val,seconds, TimeUnit.SECONDS);
    }
    public static void setHash(String key,String field,String val){
        client.getMap(key).put(field,val);
    }

    //删除
    public static void delKeys(String... keys){
        client.getKeys().delete(keys);
    }
    public static void delField(String key,String field){
        client.getMap(key).remove(field);
    }
    //查询
    public static String getStr(String key){
        return client.getBucket(key).get().toString();
    }
    public static String getHash(String key,String field){
        return client.getMap(key).get(field).toString();
    }
    public static boolean checkField(String key,String field){
        return client.getMap(key).containsKey(field);
    }
    public static boolean isHave(String key){
        return client.getKeys().countExists(key)>0;
    }
}
